package by.dkusch.aircompany.webapp.page.flight.panel;

import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.FilterForm;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;

import by.dkusch.aircompany.model.filter.FilterRoute;
import by.dkusch.aircompany.model.tables.Route;
import by.dkusch.aircompany.model.tables.Route_;
import by.dkusch.aircompany.services.RouteService;
import by.dkusch.aircompany.webapp.page.base.BaseLayout;
import by.dkusch.aircompany.webapp.page.base.common.AddNewItemPanel;
import by.dkusch.aircompany.webapp.page.base.common.CommonActionPanel;
import by.dkusch.aircompany.webapp.page.base.common.EntityDataProviderFilter;
import by.dkusch.aircompany.webapp.page.base.common.SelectorActionPanel;
import by.dkusch.aircompany.webapp.page.base.enums.SelectorType;
import by.dkusch.aircompany.webapp.page.base.panels.MenuPanel;
import by.dkusch.aircompany.webapp.page.flight.RoutePage;

public class RouteListPanel extends Panel {

	private static final long serialVersionUID = 1L;
	@Inject
	private RouteService routeService;
	private EntityDataProviderFilter<Long, Route, FilterRoute> routeDataProvider;

	public RouteListPanel(String id, int itemsPerPage, final boolean choiseFl) {
		super(id);

		routeDataProvider = new EntityDataProviderFilter<Long, Route, FilterRoute>(routeService);
		routeDataProvider.setSort(Route_.code, SortOrder.ASCENDING);
		routeDataProvider.setFilterState(new FilterRoute());

		setOutputMarkupId(true);

		DataView<Route> dataView = new DataView<Route>("routeList", routeDataProvider, itemsPerPage) {
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(Item<Route> item) {
				final Route route = item.getModelObject();
				item.add(new CheckBox("arcFl").setEnabled(false));
				item.add(new Label("code"));
				item.add(new Label("name"));
				item.add(new Label("startPoint.code"));
				item.add(new Label("endPoint.code"));
				item.add(new Label("duration"));
				if (choiseFl) {
					item.add(new SelectorActionPanel<Long>("action-panel", route.getId(), SelectorType.ROUTE));
				} else {
					item.add(new CommonActionPanel<Long, Route, RoutePage>("action-panel", routeService, route, RoutePage.class, RouteListPanel.this));
				}

			}
		};

		FilterForm<FilterRoute> filterForm = new FilterForm<FilterRoute>("filterForm", routeDataProvider);
		filterForm.add(new TextField<String>("filter-code", PropertyModel.<String> of(routeDataProvider, "filterState.code")));
		filterForm.add(new TextField<String>("filter-name", PropertyModel.<String> of(routeDataProvider, "filterState.name")));
		filterForm.add(new TextField<String>("filter-airport-start", PropertyModel.<String> of(routeDataProvider, "filterState.startAirportCode")));
		filterForm.add(new TextField<String>("filter-airport-end", PropertyModel.<String> of(routeDataProvider, "filterState.endAirportCode")));
		filterForm.add(new TextField<String>("filter-duration-min", PropertyModel.<String> of(routeDataProvider, "filterState.durationMin")).setLabel(new ResourceModel(
				"airport.list.filter.duration.min")));
		filterForm.add(new TextField<String>("filter-duration-max", PropertyModel.<String> of(routeDataProvider, "filterState.durationMax")).setLabel(new ResourceModel(
				"airport.list.filter.duration.max")));

		final WebMarkupContainer divTable = new WebMarkupContainer("div-table");
		final PagingNavigator pagingNavigator = new PagingNavigator("paging", dataView);
		divTable.setOutputMarkupId(true);
		pagingNavigator.setOutputMarkupId(true);

		filterForm.add(new AjaxButton("route-search", filterForm) {
			/**
				 * 
				 */
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				// repaint the feedback panel so that it is hidden
				target.add(((MenuPanel) ((BaseLayout) getPage()).getMenuPanel()).getFeedback());
				target.add(divTable);
				target.add(pagingNavigator);
			}

			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {
				// repaint the feedback panel so errors are shown
				target.add(((MenuPanel) ((BaseLayout) getPage()).getMenuPanel()).getFeedback());
				target.add(divTable);
			}
		});

		divTable.add(dataView);
		AddNewItemPanel<Long, Route, RoutePage> addNewItemPanel = new AddNewItemPanel<Long, Route, RoutePage>("new-panel", new Route(), RoutePage.class);
		if (choiseFl) {
			addNewItemPanel.setVisible(false);
		}
		divTable.add(addNewItemPanel);

		divTable.add(new OrderByBorder<SingularAttribute<Route, ?>>("sortByName", Route_.name, routeDataProvider));
		divTable.add(new OrderByBorder<SingularAttribute<Route, ?>>("sortByCode", Route_.code, routeDataProvider));
		divTable.add(new OrderByBorder<SingularAttribute<Route, ?>>("sortByStartPoint", Route_.startPoint, routeDataProvider));
		divTable.add(new OrderByBorder<SingularAttribute<Route, ?>>("sortByEndPoint", Route_.endPoint, routeDataProvider));
		divTable.add(new OrderByBorder<SingularAttribute<Route, ?>>("sortByDuration", Route_.duration, routeDataProvider));

		filterForm.add(divTable);
		add(pagingNavigator);
		add(filterForm);
	}

}
